package ru.tpu.osu.faces.dao;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

@Entity
public class PrivateMessage implements Serializable, Comparable<PrivateMessage> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7289664428858249804L;
	@Id
	@Temporal(value=TemporalType.TIMESTAMP)
	private Date sentDate;
	@Id
	@ManyToOne
	private User sender;
	@Id
	@ManyToOne
	private User reciever;
	@Column
	private String text;
	@Column
	private String theme;
	@Column(nullable=false)
	private Boolean isRead = false;
	@Transient
	private User firstParty;
	
	public void setFirstParty(User firstParty) {
		this.firstParty = firstParty;
	}
	public Date getSentDate() {
		return sentDate;
	}
	public void setSentDate(Date sentDate) {
		this.sentDate = sentDate;
	}
	public User getSender() {
		return sender;
	}
	public void setSender(User sender) {
		this.sender = sender;
	}
	public User getReciever() {
		return reciever;
	}
	public void setReciever(User reciever) {
		this.reciever = reciever;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getTheme() {
		return theme;
	}
	public void setTheme(String theme) {
		this.theme = theme;
	}
	public Boolean getIsRead() {
		return isRead;
	}
	public void setIsRead(Boolean isRead) {
		this.isRead = isRead;
	}
	public int compareTo(PrivateMessage o) {
		return getSentDate().compareTo(o.getSentDate());
	}
	public User getOtherParty(){
		if (getSender().equals(firstParty)){
			return getReciever();
		}
		if (getReciever().equals(firstParty)){
			return getSender();
		}
		return null;
	}
}
